package com.weixin.common.utils;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * <p> @Title MyEncryptTypeHandler
 * <p> @Description 字段加密处理
 *
 * @author ACGkaka
 * @date 2023/2/21 17:20
 */
public class MyEncryptTypeHandler extends BaseTypeHandler<String> {

    private static final String AES_KEY = "jew0utl53jfnd43y";

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i, AESUtil.encrypt(parameter, AES_KEY));
    }

    @Override
    public String getNullableResult(ResultSet rs, String column) throws SQLException {
        return AESUtil.decrypt(rs.getString(column), AES_KEY);
    }

    @Override
    public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return AESUtil.decrypt(rs.getString(columnIndex), AES_KEY);
    }

    @Override
    public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return AESUtil.decrypt(cs.getString(columnIndex), AES_KEY);
    }
}
